package com.qf.service.impl;

import com.qf.entity.*;
import com.qf.entity.custom.DessertmenuCustom;
import com.qf.entity.custom.ShopCustom;
import com.qf.mapper.DessertmenuMapper;
import com.qf.mapper.UserAttentionMapper;
import com.qf.mapper.UserCollectionMapper;
import com.qf.mapper.UserShareMapper;
import com.qf.mapper.custom.DessertmenuCustomMapper;
import com.qf.mapper.custom.ShopCustomMapper;
import com.qf.service.DiscoverService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: ZhangYubin
 * @date: 2020/7/21 22:56
 * @description:
 */
@Service
public class DiscoverServiceImpl implements DiscoverService {
    @Autowired
    private DessertmenuMapper dessertmenuMapper;
    @Autowired
    private DessertmenuCustomMapper dessertmenuCustomMapper;
    @Autowired
    private ShopCustomMapper shopCustomMapper;
    @Autowired
    private UserAttentionMapper userAttentionMapper;
    @Autowired
    private UserShareMapper userShareMapper;
    @Autowired
    private UserCollectionMapper userCollectionMapper;

    /**
     * 查询dessertmenu表和user表，所有数据（除菜谱内容）,id，标题，图片，用户名，点击量，
     * 调用dessertmenuCustomMapper的selectDessertmenuCustomUser方法
     *
     * @return 返回一个List<DessertmenuCustom>集合，有多个DessertmenuCustom对象
     * @throws Exception
     */
    @Override
    public List<DessertmenuCustom> findAllDessertmenu() throws Exception {
        return dessertmenuCustomMapper.selectDessertmenuCustomUser();
    }

    //查询发现精品页每条详细信息

    /**
     * 在发现精品页，点击信息，从前台传回被点id，根据id查询详细信息
     *
     * @param id 对应Dessertmenu实体类的dessertmenuId
     * @return 返回一个Dessertmenu实体类型数据
     * @throws Exception
     */
    @Override
    public Dessertmenu findDessertmenu(int id) throws Exception {

        return dessertmenuMapper.selectByPrimaryKey(id);
    }

    //增加发现精品页菜谱内容
    @Override
    public int addDessertmenu(Dessertmenu dessertmenu) throws Exception {
        return dessertmenuMapper.insertSelective(dessertmenu);
    }

    //删除发现精品页菜谱内容(批量删除)
    @Override
    public int delDessertmenu(int[] ids) throws Exception {
        int result = -1;
        for (int id : ids) {
            int i = dessertmenuMapper.deleteByPrimaryKey(id);
            if (i != 1) {
                result = i;
            }
        }
        return result;
    }

    //修改发现精品页菜谱内容
    @Override
    public int updateDessertmenu(Dessertmenu dessertmenu) throws Exception {
        return dessertmenuMapper.updateByPrimaryKeySelective(dessertmenu);
    }


    /**
     * 发现关注页：前端不传参数，后端查询所有信息（店铺名， 店铺图片，信息标题，信息图片，发布用户昵称，该信息访问量）
     *
     * @return 返回一个List<ShopCustom>实体类型数据
     */
    @Override
    public List<ShopCustom> selectShopDessermenuUser() {
        return shopCustomMapper.selectShopDessermenuUser();
    }

    /**
     * 发现关注页：点击关注是，在user_attention添加数据
     * 判断插入之后变化行数，不为0为true，为0为false
     *
     * @param userAttention 实体类包含user_id和shop_id
     * @return 返回一个添加成功或者失败的true or false
     */
    @Override
    public boolean addUserattention(UserAttention userAttention) {
        int i = userAttentionMapper.insertSelective(userAttention);
        if (i != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 查询user_share表，晒单名称， 晒单图片根据点赞数排序
     * 调用userShareMapper的selectByExample方法
     *
     * @return 返回一个List<UserShare>集合，有多个UserShare对象
     * @throws Exception
     */
    @Override
    public List<UserShare> findAllShareOrderByPoint() {
        UserShareExample userShareExample = new UserShareExample();
        userShareExample.setOrderByClause("Share_Point_Counts");
        return userShareMapper.selectByExample(userShareExample);
    }

    /**
     * 发现晒单页：添加收藏，在user_collection添加收藏
     * 判断插入之后变化行数，不为0为true，为0为false
     *
     * @param userCollection 实体类包含
     * @return 添加成功返回true 失败返回false
     */
    @Override
    public boolean addUserCollection(UserCollection userCollection) {
        int i = userCollectionMapper.insertSelective(userCollection);
        if (i != 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 发现晒单页：前台传递shareId，调用userShareMapper的添加方法来增加数据
     *
     * @param userShare 包含shareUserId，shareImages，shareWords的userShare的实体类
     * @return 添加成功返回true 失败返回false
     */
    @Override
    public boolean addUserShare(UserShare userShare) {

        int i = userShareMapper.insertSelective(userShare);
        if (i != 0) {
            return true;
        } else {
            return false;
        }

    }

    /**
     * 发现晒单页：前台传递shareId，调用userShareMapper的修改方法来增加sharePointCounts
     *
     * @param userShare 包含shareCollectCounts属性值的实体类
     * @return 修改成功返回true 失败返回false
     */
    @Override
    public boolean modifyUserShare(UserShare userShare) {
        int shareId = userShare.getShareId();
        UserShare userShare1 = userShareMapper.selectByPrimaryKey(shareId);
        Integer sharePointCounts = userShare1.getSharePointCounts();
        if (sharePointCounts != null) {
            int num = ++sharePointCounts;
            userShare.setSharePointCounts(num);
            int i = userShareMapper.updateByPrimaryKeySelective(userShare);
            if (i != 0) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }

    }

    /**
     * 根据isnew查询shop表和product表
     * 调用shopCustomMapper的selectShopProductisnew方法
     * @return 返回一个List<ShopCustom>
     */
    @Override
    public List<ShopCustom> findAllShopProductIsNew() {
        return shopCustomMapper.selectShopProductisnew();
    }

    /**
     * 根据isrecommend查询shop表和product表
     * 调用shopCustomMapper的selectShopProductisrecommend方法
     * @return 返回一个List<ShopCustom>
     */
    @Override
    public List<ShopCustom> findAllShopProductIsRecommend() {
        return shopCustomMapper.selectShopProductisrecommend();
    }
}
